package leetcode;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: 合并两个有序链表
 * @date 2022/9/11 16:59
 */
public class Code21 extends ListNode{


    public static void main(String[] args) {

    }


    public ListNode mergeTwoLists(ListNode list1, ListNode list2){

        ListNode newList = new ListNode(0);

        ListNode p = newList;

        if(list1 == null){
            return list2;
        }
        if(list2 == null){
            return list1;
        }
        while(list1!=null&&list2!=null){
            if(list1.val <= list2.val){
                p.next = list1;
                list1 = list1.next;
            }else if(list2.val <= list2.val){
                p.next = list2;
                list2 = list2.next;
            }
            p = p.next;
        }
        // 目的是为了防止list1与list2不是等长的链表
        p.next = list1 == null ? list2 : list1;
        return p.next;








    }




}
